﻿using Microsoft.AspNet.SignalR.Hosting;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.StatementManage;
using SmartWMS.Server.BusinessServer.SalesManagementService.Dto;
using SmartWMS.Server.Repository;
using SmartWMS.Server.StatementServer.StartEndServer.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.StatementServer.StartEndServer
{
	/// <summary>
	/// 期初期末
	/// </summary>
	public class StartEndServer : IStartEndServer
	{
		private readonly IRepository<Product> _productRepository;
		private readonly IRepository<Category> _categoryRepository;
		private readonly IRepository<Warehouse> _warehouseRepository;
		private readonly IRepository<StartEnd> _startEndRepository;
		private readonly IRepository<InventoryBook> _inventoryBookRepository;

		public StartEndServer(IRepository<Product> productRepository, IRepository<Category> categoryRepository, IRepository<Warehouse> warehouseRepository, IRepository<StartEnd> startEndRepository, IRepository<InventoryBook> inventoryBookRepository)
		{
			_productRepository = productRepository;
			_categoryRepository = categoryRepository;
			_warehouseRepository = warehouseRepository;
			_startEndRepository = startEndRepository;
			_inventoryBookRepository = inventoryBookRepository;
		}

		/// <summary>
		/// 显示期初期末
		/// </summary>
		/// <param name="dto">要查询的条件</param>
		/// <returns></returns>
		public async Task<(int pageCount, int totalCount, Response<List<StartEndDto>> data)> GetStartEndDtoAsync(SearchDataStartendDto dto)
		{
			try
			{
				var ProductRepositoryList = await _productRepository.GetAllListAsync();
				var CategoryRepositoryList = await _categoryRepository.GetAllListAsync();
				var WarehouseRepositoryList = await _warehouseRepository.GetAllListAsync();
				var StartEndRepositoryList = await _startEndRepository.GetAllListAsync();
				var InventoryBookRepositoryList = await _inventoryBookRepository.GetAllListAsync();

				var list = (from product in ProductRepositoryList
							join category in CategoryRepositoryList
							on product.ProductTypeId equals category.Id
							join startend in StartEndRepositoryList
							on product.ProductCode equals startend.ProductCode
							join warehouse in WarehouseRepositoryList
							on startend.WarehouseId equals warehouse.Id
							join inventorybook in InventoryBookRepositoryList
							on product.ProductCode equals inventorybook.ProductCode
							select new StartEndDto
							{
								StartEndDate = startend.StartEndDate,
								ProductCode = product.ProductCode,
								ProductName = product.ProductName,
								Specs = product.Specs,
								CategoryName = category.CategoryName,
								Unit = product.Unit,
								Name = warehouse.Name,
								Start = startend.Start,
								End = startend.End,
								Delivery = startend.Delivery,
								Storage = startend.Storage,
								WaarehouseId = warehouse.Id
							}).Distinct().ToList();

				if (!string.IsNullOrEmpty(dto.ProductCode))
				{
					list = list.Where(x => x.ProductCode.Contains(dto.ProductCode)).ToList();
				}

				if (!string.IsNullOrEmpty(dto.ProductName))
				{
					list = list.Where(x => x.ProductName.Contains(dto.ProductName)).ToList();
				}

				if (!string.IsNullOrEmpty(dto.ProductBatch))
				{
					list = list.Where(x => x.ProductCode.Contains(dto.ProductBatch)).ToList();
				}

				if (dto.WarehouseId > 0)
				{
					list = list.Where(x => x.WaarehouseId == dto.WarehouseId).ToList();
				}

				//计算总页数
				int totalCount = list.Count();
				int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.pageSize);
				list = list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();

				var result = list.MapToList<StartEndDto>();

				return (pageCount, totalCount, Response<List<StartEndDto>>.Ok(result));
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
